package com.ceba.test.modules.filemanage.dao;

import abc.data.DataRows;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import abc.ID;
import com.ceba.base.exception.CebaException;
import com.ceba.base.helper.LoggerHelper;
import com.ceba.base.web.dao.IBaseDao;
import com.ceba.cebautils.DateUtil;
import com.ceba.cebautils.StringUtil;
import com.ceba.test.base.configs.CebaConfig;
import com.ceba.test.base.configs.CebaPathUtil;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ceba.test.modules.filemanage.dto.VFileObject;
import com.ceba.test.modules.filemanage.entity.GFile;
import com.ceba.test.modules.filemanage.entity.GFileRelaction;
import com.ceba.test.modules.filemanage.entity.GFileRelactionDelete;
import com.ceba.test.modules.filemanage.entity.GFolder;

import abc.data.DataRow;
import abc.data.DataTable;
import abc.data.Sql;
import abc.data.SqlSessionManager;
import abc.data.dataSource.SqlDataSource;
import abc.data.store.IStore;
import abc.data.store.StoreManager;
import abc.object.IObject;
import abc.object.XObject;
import abc.user.IUser;

public class GFileDao extends IBaseDao {
	
	static Logger logger = LoggerFactory.getLogger(GFileDao.class);
	private String tableName = "g_file";

	
	public String fileIdGetVirtualPath(String g_fileId) throws Exception{
		return getFieldValue("g_file",g_fileId,"VIRTUAL_PATH");
	}
	
	/*
	 * 拷贝datarow对象值到VFile,gFile相关的属性
	 */
	protected void copyGFileDataRowToVFileObject(DataRow dr,VFileObject vO){
		JSONObject dd = dr.toJSONObject();
		vO.setId(dd.has("ID")?dr.getString("ID"):"");
		vO.setClassS(dd.has("CLASS")?dr.getString("CLASS"):"");
		vO.setContentType(dd.has("CONTENT_TYPE")?dr.getString("CONTENT_TYPE"):"");
		vO.setDocumentType(dd.has("DOCUMENT_TYPE")?new Double(dr.getDouble("DOCUMENT_TYPE")).intValue():0);
		vO.setExtName(dd.has("EXT_NAME")?dr.getString("EXT_NAME"):"");
		vO.setFileName(dd.has("FILE_NAME")?dr.getString("FILE_NAME"):"");
		vO.setFilePath(dd.has("FILE_PATH")?dr.getString("FILE_PATH"):"");
		vO.setVirtualPath(dd.has("VIRTUAL_PATH")?dr.getString("VIRTUAL_PATH"):"");
		vO.setScope(dd.has("SCOPE")?(new Double(dr.getDouble("SCOPE")).toString()):"");
		vO.setSize(dd.has("SIZE")?dr.getLong("SIZE"):0);
		vO.setTextContent(dd.has("TEXT_CONTENT")?dr.getString("TEXT_CONTENT"):"");
		vO.setRemark(dd.has("REMARK")?dr.getString("REMARK"):"");
		vO.setCreateTime(dd.has("CREATE_TIME")?
				DateUtil.dateTrans(dr.getDate("CREATE_TIME").toString(),DateUtil.YYYY_MM_DD_HH_MM):"");
		vO.setCreateUser(dd.has("CREATE_USER")?dr.getString("CREATE_USER"):"");
		vO.setQrCodeFileId(dd.has("QRCODE_FILE_ID")?dr.getString("QRCODE_FILE_ID"):"");
		vO.setVersion(dd.has("VERSION")?dr.getString("VERSION"):"");
		vO.setDataVersion(dd.has("DATA_VERSION")?dr.getString("DATA_VERSION"):"");
		vO.setUpdateTime(dd.has("UPDATE_TIME") ?
				DateUtil.dateTrans(dr.getDate("UPDATE_TIME").toString(),DateUtil.YYYY_MM_DD_HH_MM) : "");
		vO.setStorageAddressId(dd.has("STORAGE_ADDRESS_ID")?dr.getString("STORAGE_ADDRESS_ID"):CebaConfig.FILE_STORAGE_DEFAULT_ID);
		vO.setUpdateUser(dd.has("UPDATE_USER")?dr.getString("UPDATE_USER"):"");
	}
	
	/*
	 * 拷贝datarow对象值到GFile
	 */
	public void copyDataRowToGFile(DataRow dr,GFile gFile){
		JSONObject dd = dr.toJSONObject();
		gFile.setId(dd.has("ID")?dr.getString("ID"):"");
		gFile.setClassS(dd.has("CLASS")?dr.getString("CLASS"):"");
		gFile.setContentType(dd.has("CONTENT_TYPE")?dr.getString("CONTENT_TYPE"):"");
		gFile.setDocumentType(dd.has("DOCUMENT_TYPE")?new Double(dr.getDouble("DOCUMENT_TYPE")).intValue():0);
		gFile.setExtName(dd.has("EXT_NAME")?dr.getString("EXT_NAME"):"");
		gFile.setFileName(dd.has("FILE_NAME")?dr.getString("FILE_NAME"):"");
		gFile.setFilePath(dd.has("FILE_PATH")?dr.getString("FILE_PATH"):"");
		gFile.setVirtualPath(dd.has("VIRTUAL_PATH")?dr.getString("VIRTUAL_PATH"):"");
		gFile.setScope(dd.has("SCOPE")?(new Double(dr.getDouble("SCOPE")).toString()):"");
		gFile.setSize(dd.has("SIZE")?dr.getLong("SIZE"):0);
		gFile.setTextContent(dd.has("TEXT_CONTENT")?dr.getString("TEXT_CONTENT"):"");
		gFile.setRemark(dd.has("REMARK")?dr.getString("REMARK"):"");
		gFile.setCreateTime(dd.has("CREATE_TIME")?
				DateUtil.dateTrans(dr.getDate("CREATE_TIME").toString(),DateUtil.YYYY_MM_DD_HH_MM):"");
		gFile.setCreateUser(dd.has("CREATE_USER")?dr.getString("CREATE_USER"):"");
		gFile.setQrCodeFileId(dd.has("QRCODE_FILE_ID")?dr.getString("QRCODE_FILE_ID"):"");
		gFile.setVersion(dd.has("VERSION")?dr.getString("VERSION"):"");
		gFile.setDataVersion(dd.has("DATA_VERSION")?dr.getString("DATA_VERSION"):"");
		gFile.setUpdateTime(dd.has("UPDATE_TIME") ?
				DateUtil.dateTrans(dr.getDate("UPDATE_TIME").toString(),DateUtil.YYYY_MM_DD_HH_MM) : "");
		gFile.setUpdateUser(dd.has("UPDATE_USER")?dr.getString("UPDATE_USER"):"");
		gFile.setStorageAddressId(dd.has("STORAGE_ADDRESS_ID")?dr.getString("STORAGE_ADDRESS_ID"):CebaConfig.FILE_STORAGE_DEFAULT_ID);
	}
	
	/*
	 * 拷贝GFile对象值给VFile
	 */
	public void copyGFileToVFile(GFile gFile, VFileObject vO) {
		vO.setId(gFile.getId());
		vO.setClassS(gFile.getClassS());
		vO.setContentType(gFile.getContentType());
		vO.setDocumentType(gFile.getDocumentType());
		vO.setExtName(gFile.getExtName());
		vO.setFileName(gFile.getFileName());
		vO.setFilePath(gFile.getFilePath());
		vO.setVirtualPath(gFile.getVirtualPath());
		vO.setScope(gFile.getScope());
		vO.setSize(gFile.getSize());
		vO.setTextContent(gFile.getTextContent());
		vO.setRemark(gFile.getRemark());
		vO.setCreateTime(gFile.getCreateTime());
		vO.setCreateUser(gFile.getCreateUser());
		vO.setQrCodeFileId(gFile.getQrCodeFileId());
		vO.setVersion(gFile.getVersion());
		vO.setDataVersion(gFile.getDataVersion());
	}
	
	/*
	 * 拷贝datarow对象值到VFile,GFileRelaction相关的属性
	 */
	public void copyGFileRelactionDataRowToVFileObject(DataRow dr, VFileObject vO) {
		JSONObject dd = dr.toJSONObject();
		vO.setCreateTime(dd.has("CREATE_TIME") ?
				DateUtil.dateTrans(dr.getDate("CREATE_TIME").toString(),DateUtil.YYYY_MM_DD_HH_MM): "");
		vO.setCreateUser(dd.has("CREATE_USER")?dr.getString("CREATE_USER"):"");
		vO.setDataVersion(dd.has("DATA_VERSION")?dr.getString("DATA_VERSION"):"");
		vO.setFileId(dd.has("FILE_ID")?dr.getString("FILE_ID"):"");
		vO.setFileName(dd.has("FILE_NAME")?dr.getString("FILE_NAME"):"");
		vO.setFlag(dd.has("FLAG")?dr.getString("FLAG"):"");
		vO.setFolderId(dd.has("FOLDER_ID")?dr.getString("FOLDER_ID"):"");
		vO.setId(dd.has("ID")?dr.getString("ID"):"");
		vO.setItemIndex(dr.getLong("ITEM_INDEX"));
		vO.setRFlag(dd.has("RFLAG")?dr.getLong("RFLAG"):0);
		vO.setRId(dd.has("RID")?dr.getString("RID"):"");
		vO.setSignFileId(dd.has("SIGN_FILE_ID")?dr.getString("SIGN_FILE_ID"):"");
		vO.setUpdateTime(dd.has("UPDATE_TIME")  ?
				DateUtil.dateTrans(dr.getDate("UPDATE_TIME").toString(),DateUtil.YYYY_MM_DD_HH_MM) : "");
		vO.setUpdateUser(dd.has("UPDATE_USER")?dr.getString("UPDATE_USER"):"");
		vO.setUserId(dd.has("USER_ID")?dr.getString("USER_ID"):"");
		vO.setVersion(dd.has("VERSION")?dr.getString("VERSION"):"");
		
		
		vO.setFilePath(dd.has("FILE_PATH")?dr.getString("FILE_PATH"):"");
		vO.setVirtualPath(dd.has("VIRTUAL_PATH")?dr.getString("VIRTUAL_PATH"):"");
		vO.setSize(dd.has("SIZE")?dr.getLong("SIZE"):0);
		vO.setClassS(dd.has("CLASS")?dr.getString("CLASS"):"");
		vO.setType(dd.has("TYPE")?dr.getString("TYPE"):"");
	}
	
	/*
	 * 拷贝datarow对象值到GFileRelaction
	 */
	protected void copyDrToGFileRelaction(DataRow dr,GFileRelaction gr){
		JSONObject dd = dr.toJSONObject();
		gr.setCreateTime(dd.has("CREATE_TIME") ?
				DateUtil.dateTrans(dr.getDate("CREATE_TIME").toString(),DateUtil.YYYY_MM_DD_HH_MM): "");
		gr.setCreateUser(dd.has("CREATE_USER")?dr.getString("CREATE_USER"):"");
		gr.setDataVersion(dd.has("DATA_VERSION")?dr.getString("DATA_VERSION"):"");
		gr.setFileId(dd.has("FILE_ID")?dr.getString("FILE_ID"):"");
		gr.setFileName(dd.has("FILE_NAME")?dr.getString("FILE_NAME"):"");
		gr.setFlag(dd.has("FLAG")?dr.getString("FLAG"):"");
		gr.setFolderId(dd.has("FOLDER_ID")?dr.getString("FOLDER_ID"):"");
		gr.setId(dd.has("ID")?dr.getString("ID"):"");
		gr.setItemIndex(dd.has("ITEM_INDEX")?dr.getLong("ITEM_INDEX"):0);
		gr.setRFlag(dd.has("RFLAG")?dr.getLong("RFLAG"):0);
		gr.setRId(dd.has("RID")?dr.getString("RID"):"");
		gr.setSignFileId(dd.has("SIGN_FILE_ID")?dr.getString("SIGN_FILE_ID"):"");
		gr.setUpdateTime(dd.has("UPDATE_TIME")  ?
				DateUtil.dateTrans(dr.getDate("UPDATE_TIME").toString(),DateUtil.YYYY_MM_DD_HH_MM) : "");
		gr.setUpdateUser(dd.has("UPDATE_USER")?dr.getString("UPDATE_USER"):"");
		gr.setUserId(dd.has("USER_ID")?dr.getString("USER_ID"):"");
		gr.setVersion(dd.has("VERSION")?dr.getString("VERSION"):"");
		
		gr.setFilePath(dd.has("FILE_PATH")?dr.getString("FILE_PATH"):"");
		gr.setVirtualPath(dd.has("VIRTUAL_PATH")?dr.getString("VIRTUAL_PATH"):"");
		gr.setSize(dd.has("SIZE")?dr.getLong("SIZE"):0);
		gr.setClassS(dd.has("CLASS")?dr.getString("CLASS"):"");
		gr.setType(dd.has("TYPE")?dr.getString("TYPE"):"");
		gr.setStorageAddressId(dd.has("STORAGE_ADDRESS_ID")?dr.getString("STORAGE_ADDRESS_ID"): CebaConfig.FILE_STORAGE_DEFAULT_ID);
	}	
	
	/**
	 * 拷贝gfilerelaction对象值给VFile
	 * @param gfr
	 * @param vO
	 */
	public void copyGFileRelactionToVFile(GFileRelaction gfr, VFileObject vO) {
		vO.setCreateTime(gfr.getCreateTime());
		vO.setCreateUser(gfr.getCreateUser());
		vO.setDataVersion(gfr.getDataVersion());
		vO.setFileId(gfr.getFileId());
		vO.setFileName(gfr.getFileName());
		vO.setFlag(gfr.getFlag());
		vO.setFolderId(gfr.getFolderId());
		vO.setId(gfr.getId());
		vO.setItemIndex(gfr.getItemIndex());
		vO.setRFlag(gfr.getRFlag());
		vO.setRId(gfr.getRId());
		vO.setSignFileId(gfr.getSignFileId());
		vO.setUpdateTime(gfr.getUpdateTime());
		vO.setUpdateUser(gfr.getUpdateUser());
		vO.setUserId(gfr.getUserId());
		vO.setVersion(gfr.getVersion());
		
		
		vO.setFilePath(gfr.getFilePath());
		vO.setVirtualPath(gfr.getVirtualPath());
		vO.setSize(gfr.getSize());
		vO.setClassS(gfr.getClassS());
		vO.setType(gfr.getType());
	}
	
	/*
	 * 拷贝datarow对象值到GFolder
	 */
	public void copyDrToGFolder(DataRow dr, GFolder gFolder) {
		JSONObject dd = dr.toJSONObject();
		gFolder.setClassS(dd.has("CLASS")?dr.getString("CLASS"):"");
		gFolder.setCreateTime(dd.has("CREATE_TIME") ?
				DateUtil.dateTrans(dr.getDate("CREATE_TIME").toString(),DateUtil.YYYY_MM_DD_HH_MM): "");
		gFolder.setCreateUser(dd.has("CREATE_USER")?dr.getString("CREATE_USER"):"");
		gFolder.setFolderName(dd.has("FOLDER_NAME")?dr.getString("FOLDER_NAME"):"");
		gFolder.setId(dd.has("ID")?dr.getString("ID"):"");
		gFolder.setImg(dd.has("IMG")?dr.getString("IMG"):"");
		gFolder.setName(dd.has("NAME")?dr.getString("NAME"):"");
		gFolder.setPId(dd.has("PID")?dr.getString("PID"):"" );
		gFolder.setReadSql(dd.has("READ_SQL")?dr.getString("READ_SQL"):"");
		gFolder.setRelationFolder(dd.has("RELATION_FOLDER")?dr.getString("RELATION_FOLDER"):"");
		gFolder.setReportId(dd.has("REPORT_ID")?dr.getString("REPORT_ID"):"");
		gFolder.setUserId(dd.has("USER_ID")?dr.getString("USER_ID"):"");
		gFolder.setFileName(dd.has("FILE_NAME")?dr.getString("FILE_NAME"):"");
		gFolder.setFilePath(dd.has("FILE_PATH")?dr.getString("FILE_PATH"):"");
		gFolder.setType(dd.has("TYPE")?dr.getString("TYPE"):"");
	}
	
	/*
	 * 拷贝datarow对象值到VFile,GFolder相关的属性
	 */
	public void copyGFolderDataRowToVFileObject(DataRow dr, VFileObject vO) {
		JSONObject dd = dr.toJSONObject();
		vO.setClassS(dd.has("CLASS")?dr.getString("CLASS"):"");
		vO.setCreateTime(dd.has("CREATE_TIME") ?
				DateUtil.dateTrans(dr.getDate("CREATE_TIME").toString(),DateUtil.YYYY_MM_DD_HH_MM): "");
		vO.setCreateUser(dd.has("CREATE_USER")?dr.getString("CREATE_USER"):"");
		vO.setFolderName(dd.has("FOLDER_NAME")?dr.getString("FOLDER_NAME"):"");
		vO.setId(dd.has("ID")?dr.getString("ID"):"");
		vO.setImg(dd.has("IMG")?dr.getString("IMG"):"");
		vO.setName(dd.has("NAME")?dr.getString("NAME"):"");
		vO.setPId(dd.has("PID")?dr.getString("PID"):"" );
		vO.setReadSql(dd.has("READ_SQL")?dr.getString("READ_SQL"):"");
		vO.setRelationFolder(dd.has("RELATION_FOLDER")?dr.getString("RELATION_FOLDER"):"");
		vO.setReportId(dd.has("REPORT_ID")?dr.getString("REPORT_ID"):"");
		vO.setUserId(dd.has("USER_ID")?dr.getString("USER_ID"):"");
		vO.setFileName(dd.has("FILE_NAME")?dr.getString("FILE_NAME"):"");
		vO.setFilePath(dd.has("FILE_PATH")?dr.getString("FILE_PATH"):"");
		vO.setType(dd.has("TYPE")?dr.getString("TYPE"):"");
	}
	
	/*
	 * 拷贝gFolder对象值给VFile
	 */
	public void copyGFolderToVFile(GFolder gf, VFileObject vO) {
		vO.setClassS(gf.getClassS());
		vO.setCreateTime(gf.getCreateTime());
		vO.setCreateUser(gf.getCreateUser());
		vO.setFolderName(gf.getFolderName());
		vO.setId(gf.getId());
		vO.setImg(gf.getImg());
		vO.setName(gf.getName());
		vO.setPId(gf.getPId());
		vO.setReadSql(gf.getReadSql());
		vO.setRelationFolder(gf.getRelationFolder());
		vO.setReportId(gf.getReportId());
		vO.setUserId(gf.getUserId());
		vO.setFileName(gf.getFileName());
		vO.setFilePath(gf.getFilePath());
		vO.setType(gf.getType());
	}
	
	/**
	 * 添加纪录
	 * @param gFile
	 * @return
	 */
	public boolean addFile(GFile gFile) throws Exception{
		IStore store = StoreManager.create(tableName);
		IObject object = new XObject(tableName);
		object.put("ID",gFile.getId());
		object.put("CONTENT_TYPE",gFile.getContentType());
		object.put("FILE_PATH",gFile.getFilePath());
		object.put("FILE_NAME",gFile.getFileName());
		object.put("EXT_NAME",gFile.getExtName());
		object.put("VIRTUAL_PATH",gFile.getVirtualPath());
		object.put("SIZE",gFile.getSize());
		object.put("DOCUMENT_TYPE",gFile.getDocumentType());
		object.put("CREATE_TIME",DateUtil.getNowTime(null));
		object.put("CREATE_USER",gFile.getCreateUser());
		object.put("SCOPE",gFile.getScope());
		object.put("CLASS",gFile.getClassS());
		object.put("REMARK",gFile.getRemark());
		object.put("VERSION", gFile.getVersion());
		object.put("QRCODE_FILE_ID", gFile.getQrCodeFileId());
		object.put("STORAGE_ADDRESS_ID", gFile.getStorageAddressId());

		//将文件内容存到g_file_text_content表中 不保存文件内容-李光耀20200407
//		IStore storeContent = StoreManager.create("g_file_text_content");
//		IObject objectContent = new XObject("g_file_text_content");
		String contentId = ID.getGuid();
//		objectContent.put("ID",contentId);
//		objectContent.put("TEXT_CONTENT",gFile.getTextContent());
//		objectContent.setCreateTime();
		object.put("text_content_id",contentId);

//		try {
//			storeContent.insert(objectContent);
//		} catch (Exception e) {
//			LoggerHelper.error(logger, e, "插入文件内容失败");
//		}

		if(store.insert(object)>0){
			return true;
		} else {
			throw new CebaException("g_file表插入失败。object=" + object.toText());
		}
	}

	/*
	 * 添加gfolder
	 */
	public boolean addGFolder(GFolder gf) {
		String tb = "g_folder";
		IStore store = StoreManager.create(tb);
		IObject object = new XObject(tb);
		object.put("CLASS",gf.getClassS());
		object.put("CREATE_TIME",DateUtil.getNowTime(null));
		object.put("CREATE_USER",gf.getUserId());
		object.put("FOLDER_NAME",gf.getFolderName());
		object.put("ID",gf.getId());
		object.put("IMG",gf.getImg());
		object.put("NAME",gf.getName());
		object.put("PID",gf.getPId());
		object.put("CLASS",gf.getClassS());
		object.put("READ_SQL",gf.getReadSql());
		object.put("RELATION_FOLDER",gf.getRelationFolder());
		object.put("REPORT_ID",gf.getReportId());
		object.put("USER_ID",gf.getUserId());
		if(store.insert(object)>0){
			return true;	
		}
		return false;
	}

	/**
	 * 增加GFileRelaction对象
	 * @param gfr
	 * @return
	 */
	public boolean addGFileRelaction(GFileRelaction gfr) {
		String tb = "g_file_relaction";
		IStore store = StoreManager.create(tb);
		IObject object = new XObject(tb);
		object.put("CREATE_TIME",DateUtil.getNowTime(null));
		object.put("CREATE_USER",gfr.getCreateUser());
		object.put("DATA_VERSION",gfr.getDataVersion());
		object.put("FILE_ID",gfr.getFileId());
		object.put("FILE_NAME",gfr.getFileName());
		object.put("FLAG",gfr.getFlag());
		object.put("FOLDER_ID",gfr.getFolderId());
		object.put("ID",gfr.getId());
		object.put("ITEM_INDEX",gfr.getItemIndex());
		object.put("RFLAG",gfr.getRFlag());
		object.put("RID",gfr.getRId());
		object.put("SIGN_FILE_ID",gfr.getSignFileId());
		object.put("UPDATE_TIME",DateUtil.getNowTime(null));
		object.put("UPDATE_USER",gfr.getUpdateUser());
		object.put("USER_ID",gfr.getUserId());
		object.put("VERSION",gfr.getVersion());
		object.put("STORAGE_ADDRESS_ID", gfr.getStorageAddressId());
		
		
	/*	object.put("FILE_PATH",gfr.getFilePath());*/
	/*	object.put("VIRTUAL_PATH",gfr.getVirtualPath());*/
		/*object.put("SIZE",gfr.getSize());*/
		/*object.put("CLASS",gfr.getClassS());*/
	/*	object.put("TYPE",gfr.getType());*/
		if(store.insert(object)>0){
			return true;	
		}
		return false;
	}
	
	/**
	 * 增加GFileRelactionDelete对象
	 * @param gfrd
	 * @return
	 */
	public boolean addGFileRelactionDelete(GFileRelactionDelete gfrd) {
		String tb = "g_file_relaction_delete";
		IStore store = StoreManager.create(tb);
		IObject object = new XObject(tb);
		object.put("CREATE_TIME",DateUtil.getNowTime(null));
		object.put("CREATE_USER",gfrd.getCreateUser());
		object.put("DATA_VERSION",gfrd.getDataVersion());
		object.put("FILE_ID",gfrd.getFileId());
		object.put("FILE_NAME",gfrd.getFileName());
		object.put("FLAG",gfrd.getFlag());
		object.put("FOLDER_ID",gfrd.getFolderId());
		object.put("ID",gfrd.getId());
		object.put("ITEM_INDEX",gfrd.getItemIndex());
		object.put("RFLAG",gfrd.getRFlag());
		object.put("RID",gfrd.getRId());
		object.put("SIGN_FILE_ID",gfrd.getSignFileId());
		object.put("UPDATE_TIME",DateUtil.getNowTime(null));
		object.put("UPDATE_USER",gfrd.getUpdateUser());
		object.put("USER_ID",gfrd.getUserId());
		object.put("VERSION",gfrd.getVersion());
		object.put("STORAGE_ADDRESS_ID", gfrd.getStorageAddressId());
		
		
		/*object.put("FILE_PATH",gfrd.getFilePath());*/
		/*object.put("VIRTUAL_PATH",gfrd.getVirtualPath());*/
	/*	object.put("SIZE",gfrd.getSize());*/
	/*	object.put("CLASS",gfrd.getClassS());*/
		/*object.put("TYPE",gfrd.getType());*/
		if(store.insert(object)>0){
			return true;	
		}
		return false;
	}
	
	/**
	 * 根据id获取
	 * @param fileId
	 * @return
	 */
	public GFile getGFileByFileId(String fileId){
		GFile gFile = new GFile();
		Sql sql = new Sql("select gf.ID,gfr.FILE_NAME,gf.FILE_PATH,gf.CONTENT_TYPE,"
				+ " gf.EXT_NAME,gf.VIRTUAL_PATH,gf.SIZE "
				+ " from "+tableName +" as gf"
				+ " LEFT OUTER JOIN g_file_relaction AS gfr ON gfr.FILE_ID=gf.ID"
				+ " where gf.ID = '"+fileId+"'");
		DataTable dt = SqlSessionManager.getDataTable(sql);
		if(dt !=null && dt.getRowCount() >0){
			DataRow dr = dt.getRow(0);
			gFile.setId(dr.getString("ID"));
			gFile.setContentType(dr.getString("CONTENT_TYPE"));
			gFile.setFileName(dr.getString("FILE_NAME"));
			gFile.setFilePath(dr.getString("FILE_PATH"));
			gFile.setExtName(dr.getString("EXT_NAME"));
			gFile.setVirtualPath(dr.getString("VIRTUAL_PATH"));
			gFile.setSize(dr.getLong("SIZE"));
			return gFile;
		}
		return null;
	}
	/**
	 * 删除
	 * @param invalidFileId
	 * @return
	 * @throws Exception
	 */
	public boolean deleteFileByFileId(String invalidFileId) throws Exception{
		Sql sql = new Sql("delete from "+tableName + " where ID = '"+invalidFileId+"'" );
		return SqlSessionManager.execute(sql);
	}

	public GFile getGFileByGFileId(String fileId){
		GFile gFile = new GFile();
		Sql sql = new Sql("select * FROM g_file where ID = '"+fileId+"'");
		DataTable dt = SqlSessionManager.getDataTable(sql);
		if(dt != null && dt.getRowCount() >0){
			DataRow dr = dt.getRow(0);
			copyDataRowToGFile(dr,gFile);
			return gFile;
		}
		return null;
	}

	
	public GFileRelaction getGFileRelactionById(String gFileRelactionId){
		Sql sql = new Sql("select g_file_relaction.* ,g_file.VIRTUAL_PATH ,g_file.FILE_PATH FROM g_file_relaction LEFT OUTER JOIN g_file ON g_file_relaction.FILE_ID=g_file.ID  where g_file_relaction.ID ='"+gFileRelactionId+"'");
		DataTable dt = SqlSessionManager.getDataTable(sql);
		GFileRelaction gr = new GFileRelaction();
		if(dt != null && dt.getRowCount() > 0){
			DataRow dr = dt.getRow(0);
			copyDrToGFileRelaction(dr,gr);
			return gr;
		}
		return null;
	}

	/**
	 * 根据查询条件获取文件列表
	 * @param condition
	 * @param fields
	 * @return
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public List<GFileRelaction> getGFileRelactionListByConditon(String condition,String fields){
		DataTable dt = SqlSessionManager.getDataTable(" select "+fields+" FROM g_file_relaction WHERE " + condition);
		List<GFileRelaction> list = new ArrayList();
		if(dt != null && dt.getRowCount() > 0){
			for( int i = 0 ; i < dt.getRowCount() ; i ++){
				GFileRelaction gfr = new GFileRelaction();
				copyDrToGFileRelaction(dt.getRow(i), gfr);
				list.add(gfr);
			}
		}
		return list;
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public List<GFileRelaction> getGFileRelactionListBySql(String sql) {
		DataTable dt = SqlSessionManager.getDataTable(sql);
		List<GFileRelaction> list = new ArrayList();
		if(dt != null && dt.getRowCount() > 0){
			for( int i = 0 ; i < dt.getRowCount() ; i ++){
				GFileRelaction gfr = new GFileRelaction();
				copyDrToGFileRelaction(dt.getRow(i), gfr);
				list.add(gfr);
			}
		}
		return list;
	}
	
	/**
	 * 更新一个relation文件的相关的签名Id
	 * 
	 * @param fileRelationId
	 * @param signFileId 签名文件的g_file.ID
	 * @return
	 * @throws Exception
	 */

	public Boolean updataRelationFileSign(String fileRelationId,String signFileId){
		try {
			if (StringUtil.isEmptyString(fileRelationId)) {
				return false;
			}
			return updateTableFiledValueById("g_file_relaction",fileRelationId,"SIGN_FILE_ID",signFileId);

		} catch (Exception e) {
			LoggerHelper.error(logger, e, "");
		}

		return false;
	}

	
	/**
	 * 根据查询条件获取文件目录
	 * @param condition
	 * @param fields
	 * @return
	 */
	public GFolder getGFolderByConditon( String condition ,String fields){
		DataTable dt = SqlSessionManager.getDataTable(" select "+fields+" FROM g_folder WHERE " + condition);
		GFolder gFolder = new GFolder();
		if(dt != null && dt.getRowCount() > 0){
			DataRow dr = dt.getRow(0);
			copyDrToGFolder(dr, gFolder);
			return gFolder;
		}
		return null;
	}
	
	/**
	 * 根据查询条件获取文件列表
	 * @param condition
	 * @param fields
	 * @return
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public List<GFolder> getFolderListByConditon(String condition,String fields){
		DataTable dt = SqlSessionManager.getDataTable(" select "+fields+" FROM g_folder WHERE " + condition);
		List<GFolder> list = new ArrayList();
		if(dt != null && dt.getRowCount() > 0){
			for( int i = 0 ; i < dt.getRowCount() ; i ++){
				GFolder gFolder = new GFolder();
				copyDrToGFolder(dt.getRow(i), gFolder);
				list.add(gFolder);
			}
		}
		return list;
	}
	
	/**
	 * 根据查询条件获取文件
	 * @param condition
	 * @return
	 */
	public GFile getGFileByConditon( String condition ,String fields){
		DataTable dt = SqlSessionManager.getDataTable(" select "+fields+" FROM g_file WHERE " + condition);
		GFile gFile = new GFile();
		if(dt != null && dt.getRowCount() > 0){
			DataRow dr = dt.getRow(0);
			copyDataRowToGFile(dr, gFile);
			return gFile;
		}
		return null;
	}

	/**
	 * 根据查询条件获取文件关联
	 * @param condition
	 * @return
	 */
	public GFileRelaction getGFileRelactionByConditon( String condition ,String fields) {
		DataTable dt = SqlSessionManager.getDataTable(" select "+fields+" FROM g_file_relaction WHERE " + condition);
		GFileRelaction gfr = new GFileRelaction();
		if(dt != null && dt.getRowCount() > 0){
			DataRow dr = dt.getRow(0);
			copyDrToGFileRelaction(dr, gfr);
			return gfr;
		}
		return null;
	}

	/**
	 * 更新目录对应的reportId
	 * @param reportId
	 * @return
	 */
	public boolean updateFolderReportId(String reportId) {
		return SqlSessionManager.execute("update g_folder set REPORT_ID = '"+reportId+"'");
	}

	public List<String> getAllUploadGFileVirtualPath(String startTime, String endTime) {
		Sql sql = new Sql("select VIRTUAL_PATH from " + tableName + " where CREATE_TIME>? and CREATE_TIME<?");
		sql.addParameter(startTime);
		sql.addParameter(endTime);
		DataTable dataTable = SqlSessionManager.getDataTable(sql);
		List<String> virtualPaths = new ArrayList<>();
		DataRows dataRows = dataTable.getRows();
		if (dataRows.size() > 0) {
			dataRows.forEach( row -> {
				virtualPaths.add(row.getString("VIRTUAL_PATH"));
			});
		}
		return virtualPaths;
	}
	
}
